<include file='Index/header' />
<div class="row">
	<br>

	 <div class="col-sm-12">
		<div class="col-sm-2"></div>
		<div class="col-sm-1" style="padding-top:5px;">
			<label for="barcode" class="control-label">货号：</label>
		</div>
		<div class="col-sm-2">
			<input id="barcode" name="barcode" class="form-control"></input>
		</div>

		<div class="col-sm-2" style="padding-left:100px;padding-top:5px;">
			<label for="key" class="control-label" >关键字：</label>
		</div>
		<div class="col-sm-2">
			<input id="key" name="key" class="form-control"></input>
		</div>
		<div class="col-sm-2" style="padding-left:100px;">
    	<button class="btn btn-primary"  onclick="submitComplete()">确认补足库存</button>
    </div>

  </div>
	
	<div class="col-sm-12">
		<br>
		<div class="col-sm-2">
			<table>
			<tr>请选择仓库：</tr>
			<tr><ul id="repositoryTree" class="ztree"></ul></tr>
			<br>
			<tr>请选择商品类别：</tr>
			<tr><ul id="product_class_option_tree" class="ztree"></ul></tr>
			</table>
		</div>
		<div class="col-sm-10">
			<table id="product-table" class="table table-hover table-bordered table-striped table-striped">
				<thead>
					<tr>
						<td>大类</td>
						<td>中类</td>
						<td>子类</td>
						<td>关键字</td>
						<td>货号</td>
						<td>a仓库</td>
						<td>b仓库</td>
						<td>编号</td>
						<td>商品名称</td>
						<td>条形码货号</td>
						<td>价格</td>
						<td>上架</td>
						<td>库存</td>
						<td>排序</td>
						<td>操作</td>
					</tr>
				</thead>
				<tbody>

				</tbody>
			</table>
		</div>
	</div>
	<br>
</div>

<include file='Index/footer' />
<include file='Product/product-detail' />
<script>
$(document).ready(function() {
	loadRepositoryTree('repositoryTree', true, true);
	loadProductClassTree('product_class_option_tree', true, true);
	loadTable();		
});
var table;
function loadTable(){
	table = $('#product-table').DataTable({
		"bProcessing": true,
		"bDestroy": true,
		"sPaginationType" : "input",
		"oLanguage" : {
              "sLengthMenu": "每页显示 _MENU_ 条记录",
              "sZeroRecords": "抱歉， 没有找到",
              "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
              "sInfoEmpty": "没有数据",
              "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
              "sZeroRecords": "没有检索到数据",
               "sSearch": "搜索: &nbsp",
              "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
              }
          }, 
		"ajax":'{:U("Admin/Product/getProduct/")}',
		"columns": [
					{ "data":'product_template'},
					{ "data":'product_template'},
					{ "data":'product_template'},
					{ "data":'product_template'},
					{ "data":'product_template'},
					{ "data":'a_repository_id'},
					{ "data":'b_repository_id'},
	        { "data": 'product_id' },
	        { "data": 'name' },
	        { "data": 'product_template' },
	        { "data": 'kj_price' },
	        { "data": 'status' },
	        { "data": 'default_amount' },
	        { "data": 'index' },
	        { "data": 'btn' }
    ],
    "columnDefs": [
	    {
	    	"targets": [0, 1, 2, 3, 4, 5, 6],
	      "visible":false
	    },
	    {
	    	"targets": 0,
	    	"render": function (data, type, row, table) {
	    		return data.class1;
	      }
	    },
	    {
	    	"targets": 1,
	    	"render": function (data, type, row, table) {
	    		return data.class2;
	      }
	    },
	    {
	    	"targets": 2,
	    	"render": function (data, type, row, table) {
	    		return data.class3;
	      }
	    },
	    {
	    	"targets": 3,
	    	"render": function (data, type, row, table) {
	    		return data.key;
	      }
	    },
	    {
	    	"targets": 9,

	    	"render": function (data, type, row, table) {
	    		return data.barcode;
	      }
	    },
	    {
	    	"targets": 11,
	    	"render": function (data, type, row, table) {
	    		return data == '0'? "是":"否";
	      }
	    },
	    {
	    	"targets": -1,
	    	"data": null,
	    	"render": function (data, type, row, table) {
	    		var btn = '下架';
	    		var status = '1';
	    		if(row.status != '0'){
						btn = '上架';
						status = '0';
	    		}

	    		if({$Think.session.admin.type} == '0'){
	    			return '<button class="btn btn-info" onclick="edit(\''+row.product_id+'\')">查看</button>';
	    		}else{
		        return '<button class="btn btn-info" onclick="edit(\''+row.product_id+'\')">编辑</button> <button class="btn btn-warning" onclick="modStatus(\''+row.product_id+'\',\''+status+'\')">'+btn+'</button>';
		      }
	      }
	    }
    ]
	});
	
	$("#key").keyup(function(){
		table.column(3).search(this.value).draw();
  });
  $("#barcode").on( 'change', function () {
    table.column(2).search(this.value).draw();
  });
}

function searchARepository(val){
	table.column(5).search('').draw();
	table.column(6).search('').draw();
	table.column(5).search(val).draw();
}

function searchBRepository(val){
	table.column(5).search('').draw();
	table.column(6).search('').draw();
	table.column(6).search(val).draw();
}

function searchClass1(product_class_id){
	table.column(0).search('').draw();
	table.column(1).search('').draw();
	table.column(2).search('').draw();
  table.column(0).search(product_class_id).draw();
}
function searchClass2(product_class_id){
	table.column(0).search('').draw();
	table.column(1).search('').draw();
	table.column(2).search('').draw();
  table.column(1).search(product_class_id).draw();
}
function searchClass3(product_class_id){
	table.column(0).search('').draw();
	table.column(1).search('').draw();
	table.column(2).search('').draw();
  table.column(2).search(product_class_id).draw();
}

function edit(product_id){
	//编辑按钮点击，弹出编辑框
	$.ajax({
			url: '{:U("Admin/Product/getProduct/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"product_id":product_id
			}
		})
		.success(function(data){
			$("input[name='product_id']").attr('value', data.product_id);
			$("input[name='name']").attr('value', data.name);
			$("input[name='subname']").attr('value', data.subname);
			$("input[name='kj_price']").attr('value', data.kj_price);
			$("input[name='default_amount']").attr('value', data.default_amount);
			$("input[name='warning_amount']").attr('value', data.warning_amount);
			$("input[name='origin_amount']").attr('value', data.origin_amount);
			$("input[name='index']").attr('value', data.index);

			$('#product-detail-modal').modal({
				backdrop : "static"
			});
	});
}

function modStatus(product_id, status){
	if(!confirm("是否确认？")){
		return;
	}
	$.ajax({
			url: '{:U("Admin/Product/updateProduct/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"product_id":product_id,
				"status":status
			}
		})
		.success(function(data){
			location.reload();
	});
}
var a_repository_id, b_repository_id;
function submitComplete(){
	//先判断是否选择仓库的tree，如果选择，弹出modal
	var treeObj = $.fn.zTree.getZTreeObj("repositoryTree");
	var nodes = treeObj.getSelectedNodes();
	if(nodes.length == 0){
    alert("请选择一个仓库再执行此操作！");
    return;
  }
	if(typeof(nodes[0].a_repository_id) == 'undefined'){
		b_repository_id = nodes[0].b_repository_id;
		a_repository_id = '';
	}else{
		a_repository_id = nodes[0].a_repository_id;
		b_repository_id = '';
	}
	
  $('#product-detail-note').modal({
		backdrop : "static"
	});
}
</script>
